package com.zt.dev.processMonitoring.controller;

import com.zt.dev.common.ServerResponse;
import com.zt.dev.processMonitoring.pojo.TaskProcess;
import com.zt.dev.processMonitoring.pojo.TaskProcessDTO;
import com.zt.dev.processMonitoring.pojo.TaskProcessDictionaries;
import com.zt.dev.processMonitoring.service.TaskProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 卜德凯
 * @date 2020/3/10 18:21
 */
@RestController
@RequestMapping("/tp")
public class TaskProcessController {

    @Autowired
    private TaskProcessService taskProcessService;


    /**
     * 创建评估任务流程
     * @param taskProcess
     * @param status(1为创建 2为选用)
     * @param dictionariesId(被选用的id)
     * @return
     */
    @RequestMapping("/createProcess")
    public ServerResponse createProcess(TaskProcess taskProcess,Integer status,Integer dictionariesId){
        if(!StringUtils.isEmpty(taskProcess) && status != 0 && status != null){
            String[] processNode = {};
            if(status == 1){
                processNode = taskProcess.getProcessNode().split("\\,");
                //创建的流程放到流程字典表（task_process_dictionaries）中以便下次直接选用
                Integer result = taskProcessService.addProcess(taskProcess.getProcessNode());
            }else if(status == 2){
                if(dictionariesId != null && dictionariesId != 0){
                    //根据被选的id获取字典表（task_process_dictionaries）中相应的流程
                    TaskProcessDictionaries dictionaries = taskProcessService.findProcess(dictionariesId);
                    processNode = dictionaries.getProcess().split("\\,");
                }
            }
            List<TaskProcess> processList = new ArrayList<>();
            TaskProcess newTaskProcess = new TaskProcess();
            for (int i = 0; i < processNode.length; i++) {
                newTaskProcess = new TaskProcess();
                newTaskProcess.setTaskId(taskProcess.getTaskId());
                newTaskProcess.setUserId(taskProcess.getUserId());
                newTaskProcess.setGroupId(taskProcess.getGroupId());
                newTaskProcess.setProcessNode(processNode[i]);
                processList.add(newTaskProcess);
            }
            //创建评估任务流程
            Integer result = taskProcessService.createProcess(processList);
            if(result > 0){
                return ServerResponse.createBySuccessMessage("评估任务流程创建成功！");
            }
            return ServerResponse.createByErrorMessage("评估任务流程创建失败！");
        }
        return ServerResponse.createByErrorMessage("参数有误！");
    }

    /**
     * 查看已有评估流程的任务
     * @return
     */
    @RequestMapping("/seeTaskProcess")
    public ServerResponse seeTaskProcess(){
        List<TaskProcessDTO> dtoList = taskProcessService.seeTaskProcess();
        if(!StringUtils.isEmpty(dtoList)){
            return ServerResponse.createBySuccess(dtoList);
        }
        return ServerResponse.createByErrorMessage("未获得到数据！");
    }

    /**
     * 根据taskId和userId查看具体的流程
     * @param taskId
     * @param userId
     * @return
     */
    @RequestMapping("/seeSpecificProcess")
    public ServerResponse seeSpecificProcess(Integer taskId,Integer userId){
        if(taskId != null && userId != null){
            List<TaskProcess> taskProcessList = taskProcessService.seeSpecificProcess(taskId,userId);
            if(!StringUtils.isEmpty(taskProcessList)){
                return ServerResponse.createBySuccess(taskProcessList);
            }
            return ServerResponse.createByErrorMessage("未获得到数据!");
        }
        return ServerResponse.createByErrorMessage("参数有误!");
    }

    /**
     * 根据流程id更新流程节点的状态
     * @param taskProcess
     * @return
     */
    public ServerResponse updateProcessStatus(TaskProcess taskProcess){
        if(!StringUtils.isEmpty(taskProcess)){
            Integer result = taskProcessService.updateProcessStatus(taskProcess);
            if(result > 0){
                return ServerResponse.createBySuccessMessage("节点状态更新成功！");
            }
            return ServerResponse.createByErrorMessage("节点状态更新失败！");
        }
        return ServerResponse.createByErrorMessage("参数有误！");
    }
}
